< previous page page_306 next page >

Page 306
Example program SierpG2 uses this code to draw a Sierpinski gasket-like curve. The program initially invokes the SierpinskiGasketCurve routine with a direction of 0 (horizontally moving to the right), a distance large enough to cross most of the picture box, and a turn angle of 60 degrees. This produces images like those shown in Figure 10.27.
Sierpinski Chaos
Another way you can create Sierpinski gaskets is using a program called the chaos game. Before playing the game, you select several anchor points within a picture box. The program starts by picking a random point anywhere within the box. Then it repeatedly selects one of the anchor points randomly and moves halfway from its currently selected point to the anchor point. It plots that position and makes that the next selected point. As the game progresses, the program sometimes creates ghostly images one pixel at a time.
The following code shows the heart of the chaos game. The arrays AnchorX and AnchorY store the coordinates of the anchor points as fractions of the size of the picCanvas picture box. For example, if AnchorX(i) = 0.1, the ith anchor point is 10 percent of the way through the picture box from left to right.
e10a6a0a59f035e870d90cca05b85fce.gif
' Play the chaos game.
Private Sub PlayGame()
Dim wid As Single
Dim hgt As Single
Dim X As Single
Dim Y As Single
Dim anchor As Integer
Dim i As Integer

    ' See how much room we have.
    wid = picCanvas.ScaleWidth
    hgt = picCanvas.ScaleHeight

    ' Pick a random starting point.
    X = wid * Rnd
    Y = hgt * Rnd

    ' Start the game.
    i = 0
    Do While Running
        ' Pick a random anchor point.
        anchor = Int(NumAnchors * Rnd + 1)

        ' Move halfway there.
        X = (X + wid * AnchorX(anchor)) / 2
        Y = (Y + hgt * AnchorY(anchor)) / 2
        picCanvas.PSet (X, Y)

 
< previous page page_306 next page >